﻿using DevComponents.DotNetBar;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Shine.SchemaManager
{
    public partial class UpdateItemForm : Office2007Form
    {
        private static UpdateItemForm instance = null;
        public delegate void ModifyProjectObj(规划项目 obj);
        public ModifyProjectObj getDelegateHandler = null;
        private 规划项目 project = null;

        private UpdateItemForm(规划项目 obj)
        {
            InitializeComponent();
            this.project = this.CopyObject(obj);
            this.FillData(project);
        }

        private 规划项目 CopyObject(规划项目 obj)
        {
            规划项目 DeepCopyObj = new 规划项目();
            DeepCopyObj.Department = obj.Department;
            DeepCopyObj.Designer = obj.Designer;
            DeepCopyObj.EndDate = obj.EndDate;
            DeepCopyObj.Operator = obj.Operator;
            DeepCopyObj.Path = obj.Path;
            DeepCopyObj.ProjectName = obj.ProjectName;
            DeepCopyObj.ProjectNum = obj.ProjectNum;
            DeepCopyObj.StartDate = obj.StartDate;
            DeepCopyObj.Status = obj.Status;
            DeepCopyObj.Type = obj.Type;
            DeepCopyObj.Zone = obj.Zone;
            return DeepCopyObj;
        }

        public static UpdateItemForm GetInstance(规划项目 obj)
        {
            if (instance == null)
            {
                instance = new UpdateItemForm(obj);
            }
            return instance;
        }

        private void FillData(规划项目 obj)
        {
            this.tbxProjectName.Text = obj.ProjectName;
            this.tbxNumber.Text = obj.ProjectNum;
            this.tbxProjectType.Text = obj.Type;
            this.tbxCompany.Text = obj.Designer;

            this.cbxStatus.Items.Clear();
            this.cbxStatus.Items.Add(ProjectStatus.NotStart);
            this.cbxStatus.Items.Add(ProjectStatus.NotFinish);
            this.cbxStatus.Items.Add(ProjectStatus.Finished);
            this.cbxStatus.SelectedText = obj.Status;
            
            this.cbxOffice.Items.Clear();
            this.cbxOffice.Items.Add(ProjectZone.zoneA);
            this.cbxOffice.Items.Add(ProjectZone.zoneB);
            this.cbxOffice.Items.Add(ProjectZone.zoneC);
            this.cbxOffice.SelectedText = obj.Zone;

            this.dtpStartDate.Value = new DateTime(
                Int32.Parse(obj.StartDate.Split('/')[0]),
                Int32.Parse(obj.StartDate.Split('/')[1]),
                Int32.Parse(obj.StartDate.Split('/')[2]));
            this.dtpEndDate.Value = new DateTime(
                Int32.Parse(obj.EndDate.Split('/')[0]),
                Int32.Parse(obj.EndDate.Split('/')[1]),
                Int32.Parse(obj.EndDate.Split('/')[2]));

            this.tbxDepartment.Text = obj.Department;
            this.tbxEmployee.Text = obj.Operator;

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ///TODO:保存XML
            this.project.ProjectName = tbxProjectName.Text.Length > 0 ? tbxProjectName.Text : "";
            this.project.ProjectNum = tbxNumber.Text.Length > 0 ? tbxNumber.Text : "";
            this.project.Type = tbxProjectType.Text.Length > 0 ? tbxProjectType.Text : "";
            this.project.Designer = tbxCompany.Text.Length > 0 ? tbxCompany.Text : "";
            if (cbxStatus.SelectedItem == null)
            {
                this.project.Status = cbxStatus.SelectedText.ToString();
            }
            else
            {
                this.project.Status = cbxStatus.SelectedItem.ToString().Length > 0 ? cbxStatus.SelectedItem.ToString() : "";
            }
            if (cbxOffice.SelectedItem == null)
            {
                this.project.Zone = cbxStatus.SelectedText.ToString();
            }
            else
            {
                this.project.Zone = cbxOffice.SelectedItem.ToString().Length > 0 ? cbxOffice.SelectedItem.ToString() : "";
            }
            
            this.project.StartDate = dtpStartDate.Value.ToShortDateString();
            this.project.EndDate = dtpEndDate.Value.ToShortDateString();
            this.project.Department = tbxDepartment.Text.Length > 0 ? tbxDepartment.Text : "";
            this.project.Operator = tbxEmployee.Text.Length > 0 ? tbxEmployee.Text : "";

            // 保存XML
            SaveFileDialog fdlg = new SaveFileDialog();
            fdlg.Title = "另存为……";
            fdlg.InitialDirectory = @"c:\";
            fdlg.Filter = "All files(*.xml) | *.xml";
            fdlg.FilterIndex = 0;
            fdlg.RestoreDirectory = false;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                //string fileXML = System.IO.Path.GetDirectoryName(fdlg.FileName) + "\\" + fdlg.FileName + ".xml";
                string fileXML = fdlg.FileName;
                if(!fileXML.Contains(".xml"))
                {
                    fileXML += ".xml";
                }
                Shine.Util.Configurations.ConfigurationHelper.SaveObjectToXml<规划项目>(project, fileXML, "Root");
            }

            MessageBox.Show("更新成功！");
            return;
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.DefaultValues();
        }

        private void DefaultValues()
        {
            this.tbxProjectName.Text = "";
            this.tbxNumber.Text = "";
            this.tbxProjectType.Text = "";
            this.tbxCompany.Text = "";
            this.cbxStatus.SelectedIndex = 0;
            this.cbxOffice.SelectedIndex = 0;
            this.dtpStartDate.ResetText();
            this.dtpEndDate.ResetText();
            this.tbxDepartment.Text = "";
            this.tbxEmployee.Text = "";
        }

        private void UpdateItemForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.getDelegateHandler != null)
            {
                this.getDelegateHandler(this.project);
            }

            this.Close();
        }
    }
}
